﻿using Hotel.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.DAL
{
    public class MemberDAL
    {
        public MemberDAL(HotelContext hotelContext)
        {
            HotelContext = hotelContext;
        }

        public HotelContext HotelContext { get; }

        /// <summary>
        /// 会员添加
        /// </summary>
        /// <returns></returns>
        public int MemberAdd(MemberModel member)
        {
            try
            {
                HotelContext.MemberInfo.Add(member);
                return HotelContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <returns></returns>
        public int LDelet(int id)
        {
            try
            {
                //1.根据id查到实体对象
                var Info = HotelContext.MemberInfo.Where(x => x.Id == id).FirstOrDefault();
                //2.修改状态
                Info.IsDeleted = true;
                //3.更新数据库
                return HotelContext.SaveChanges();

            }
            catch (Exception ex)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改 修改(反填-id查询)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MemberModel FanTianMember(int id)
        {
            try
            {
                //1.通过id查询到对象
                var mem = HotelContext.MemberInfo.Where(x => x.Id == id).FirstOrDefault();
                return mem;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改 
        /// </summary>
        /// <returns></returns>
        public int Updata(MemberModel member)
        {
            try
            {
                ////查询到旧的id
                //var mem = HotelContext.MemberInfo.Where(x => x.Id == member.Id).FirstOrDefault();
                //mem.Name = member.Name;
                //mem.Phone = member.Phone;
                //mem.Card = member.Card;
                //mem.Birthday = member.Birthday;
                //mem.Sex = member.Sex;
                //mem.State = member.State;
                //mem.Pwd = member.Pwd;
                //mem.Money = member.Money;
                //mem.Points = member.Points;
                //mem.Update = DateTime.Now;
                //mem.LId = member.LId;
                //更新数据库
                var aa = Updata(member);
                return HotelContext.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }



        /// <summary>
        /// 会员添加-验证唯一
        /// </summary>
        /// <returns></returns>
        public int WYMemberAdd(string Phone, string Card)
        {
            try
            {
                var res = HotelContext.MemberInfo.Where(x => x.Phone == Phone).Count();
                if (res > 0)
                {
                    return -1;//手机号重复
                }
                var aa = HotelContext.MemberInfo.Where(x => x.Card == Card).Count();
                if (aa > 0)
                {
                    return -2;//身份证重复  
                }
                return 1;//没有重复
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 会员等级添加
        /// </summary>
        /// <returns></returns>
        public int LevelAdd(LevelModel level)
        {
            try
            {
                HotelContext.levelInfo.Add(level);
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 判断等级名称是否相同
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int IsLeveName(string name)
        {
            try
            {
                var res = HotelContext.levelInfo.Where(x => x.LName == name).Count();
                return res;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 等级List
        /// </summary>
        /// <returns></returns>
        public List<LevelModel> LevelList()
        {
            return HotelContext.levelInfo.ToList();
        }


        /// <summary>
        /// 查询显示
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="levelId"></param>
        /// <returns></returns>
        public List<MemberModel> Memebrlist(int? id, string? name, int? levelId)
        {
            try
            {
                //查询所有显示的数据
                //var list = HotelContext.MemberInfo.Where(x => x.IsDeleted == false).ToList();
                var list = HotelContext.MemberInfo.ToList();
                //根据条件查询
                if (id != null)
                {
                    list = list.Where(x => x.Id == id).ToList();
                }
                if (!string.IsNullOrEmpty(name))
                {
                    list = list.Where(x => x.Name.Contains(name)).ToList();
                }
                //if (!string.IsNullOrEmpty(name))
                //{
                //    list = list.Where(x=>x.Name.)
                //}
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 权益添加
        /// </summary>
        /// <returns></returns>
        public int RightAdd(RightModel right)
        {
            var name = HotelContext.RightInfo.Where(x => x.Name == right.Name).FirstOrDefault();
            if (name != null)
            {
                return -1;//权益名称重复
            }
            HotelContext.RightInfo.Add(right);
            return HotelContext.SaveChanges();
        }

        /// <summary>
        /// 权益List
        /// </summary>
        /// <param name="totalConut"></param>
        /// <param name="topage"></param>
        /// <param name="name"></param>
        /// <param name="CTime"></param>
        /// <param name="ETime"></param>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public List<RightModel> RightList(out int totalConut, out int topage, string? name, DateTime? CTime, DateTime? ETime, int index = 1, int size = 3)
        {
            //1.查询所有为false 的查询
            var list = HotelContext.RightInfo.Where(x => x.IsDeleted == false).ToList();
            //2.根据名称模糊查询
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x => x.Name.Contains(name)).ToList();
            }
            //3.时间范围查询
            if (CTime != null && ETime != null)
            {
                //AddDays+1天
                list = list.Where(x => x.CreatTime >= CTime && x.CreatTime <= ETime.Value.AddDays(1)).ToList();
            }
            //分页
            //计算总条数
            totalConut = list.Count();
            //计算总页数 共:  每页    显示第几页
            topage = (int)Math.Ceiling(totalConut * 1.0 / size);
            //分页
            list = list.OrderByDescending(x => x.Id).Skip((index - 1) * size).Take(size).ToList();
            return list;
        }

        //修改会员权益状态
        public int UpdateRightstate(int id, int state)
        {
            //1.根据id找到对应的实体
            var info = HotelContext.RightInfo.Where(x=>x.Id == id).FirstOrDefault();
            //2.修改 
            info.State = state;
            info.UpTime = DateTime.Now;
            return HotelContext.SaveChanges();
        }

    }
}
